package com.zd.bms.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zd.bms.bean.vo.req.BmsUserListReqVo;
import com.zd.bms.bean.vo.resp.BmsUserRespVo;
import com.zd.bms.service.IBmsRoleService;
import com.zd.bms.service.IBmsUserService;
import com.zd.bms.service.IScenicSpotService;
import com.zd.common.bean.model.BmsUser;
import com.zd.common.common.enums.BmsUserTypeEnum;
import com.zd.common.dao.common.Page;

@Controller
@RequestMapping("user")
public class UserController extends BaseController {
	@Autowired
	private IScenicSpotService scenicSpotService;

	@Autowired
	private IBmsUserService bmsUserService;
	
	@Autowired
	private IBmsRoleService bmsRoleService;

	@RequestMapping("index")
	public ModelAndView index() throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("user/index");
		BmsUser cookieBmsUser = getSessionUser();
		if (BmsUserTypeEnum.Scenic_area_administrator.getKey() == cookieBmsUser.getType()) {
			mv.setViewName("user/scenicSpotIndex");
			Integer scenicSpotId = cookieBmsUser.getScenicSpotId();
			mv.addObject("scenicSpotId", scenicSpotId);
		} else if (BmsUserTypeEnum.Platform_user.getKey() == cookieBmsUser.getType()) {
			List<Map<String, Object>> scenicSpotList = scenicSpotService.getScenicSpotKeyValueList();
			mv.addObject("scenicSpotList", scenicSpotList);
		} else {
			return null;
		}
		return mv;
	}

	@RequestMapping("saveOrUpdateIndex")
	public ModelAndView saveOrUpdateIndex(Integer userId) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("user/saveOrUpdateIndex");
		BmsUser cookieBmsUser = getSessionUser();
		mv.addObject("bmsRoleList", bmsRoleService.findRoleForUser(cookieBmsUser).get("bmsRoleList"));
		if (BmsUserTypeEnum.Scenic_area_administrator.getKey() == cookieBmsUser.getType()) {
			mv.addObject("scenicSpotId", cookieBmsUser.getScenicSpotId());
		} else if (BmsUserTypeEnum.Platform_user.getKey() == cookieBmsUser.getType()) {
			mv.addObject("scenicSpotList", scenicSpotService.getScenicSpotKeyValueList());
		} else {
			return null;
		}
		if (userId != null) {
			BmsUser bmsUser = bmsUserService.get(userId);
			mv.addObject("bmsUser", bmsUser);
		}
		return mv;
	}

	@ResponseBody
	@RequestMapping("list")
	public Map<String, Object> list(BmsUserListReqVo reqVo) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();

		BmsUser cookieBmsUser = getSessionUser();
		if (BmsUserTypeEnum.Scenic_area_administrator.getKey() == cookieBmsUser.getType()) {
			reqVo.setScenicSpotId(cookieBmsUser.getScenicSpotId());
		}
		Page<BmsUserRespVo> bmsUserPage = bmsUserService.pageBmsUser(reqVo);
		resultMap.put("data", bmsUserPage.getList());
		resultMap.put("draw", reqVo.getDraw());
		resultMap.put("recordsTotal", bmsUserPage.getList().size());
		resultMap.put("recordsFiltered", bmsUserPage.getTotalRow());

		return resultMap;
	}

	@ResponseBody
	@RequestMapping("saveOrUpdate")
	public Map<String, Object> saveOrUpdate(BmsUser bmsUser) throws Exception {
		BmsUser cookieBmsUser = getSessionUser();
		bmsUser.setOperator(cookieBmsUser.getId());
		Map<String, Object> resultMap = bmsUserService.saveOrUpdateUser(bmsUser);
		return resultMap;
	}

	@ResponseBody
	@RequestMapping("changeStatus")
	public Map<String, Object> changeStatus(Integer userId, int status) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		BmsUser bmsUserInfo = bmsUserService.get(userId);
		if (bmsUserInfo != null) {
			bmsUserInfo.setStatus(status);
			bmsUserInfo.setUpdateTime(new Date());
			bmsUserService.update(bmsUserInfo);
			resultMap.put("result", true);
			return resultMap;
		}
		resultMap.put("result", false);
		resultMap.put("errorMsg", "用户不存在!");
		return resultMap;
	}
}
